<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="客户姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getCustomers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="customers" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="60">
            </el-table-column>
            <el-table-column prop="age" label="年龄"  width="60" sortable>
            </el-table-column>
            <el-table-column prop="gender" label="性别" width="100" :formatter="formatSex" sortable>
            </el-table-column>
            <el-table-column prop="tel" label="电话"  width="160" sortable>
            </el-table-column>
            <el-table-column prop="email" label="邮箱"  sortable>
            </el-table-column>
            <el-table-column prop="qq" label="QQ"  sortable>
            </el-table-column>
            <el-table-column prop="wechat" label="微信"  sortable>
            </el-table-column>
            <el-table-column prop="seller" label="销售专员"  sortable>
            </el-table-column>
            <el-table-column prop="job" label="职业"  sortable>
            </el-table-column>
            <el-table-column prop="salaryLevel" label="收入水平"  sortable>
            </el-table-column>
            <el-table-column prop="customerSource" label="客户来源"  sortable>
            </el-table-column>
            <el-table-column prop="inputUser" label="录入人"  sortable>
            </el-table-column>
            <el-table-column   label="录入时间" sortable>
                <!--使用过滤器的写法 scope.row.字段名-->
                <template slot-scope="scope">
                    <span>{{ scope.row.inputTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="tenant.companyName" label="所属租户"  sortable>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button  size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--分页工具条-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
            </el-pagination>
        </el-col>
        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="客户姓名" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="editForm.gender">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="电话">
                    <el-input v-model="editForm.tel"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="editForm.email"></el-input>
                </el-form-item>
                <el-form-item label="QQ">
                    <el-input v-model="editForm.qq"></el-input>
                </el-form-item>
                <el-form-item label="微信">
                    <el-input v-model="editForm.wechat"></el-input>
                </el-form-item>
                <el-form-item label="销售专员">
                    <el-input v-model="editForm.seller"></el-input>
                </el-form-item>
                <el-form-item label="职业">
                    <el-input v-model="editForm.job"></el-input>
                </el-form-item>
                <el-form-item label="收入水平">
                    <el-input v-model="editForm.salaryLevel"></el-input>
                </el-form-item>
                <el-form-item label="客户来源">
                    <el-input v-model="editForm.customerSource"></el-input>
                </el-form-item>
                <el-form-item label="录入人">
                    <el-input v-model="editForm.inputUser"></el-input>
                </el-form-item>

                <el-form-item label="录入时间">
                    <el-date-picker type="datetime" placeholder="请选择时间"
                                    v-model="editForm.inputTime"
                                    format="yyyy-MM-dd HH:mm:ss"/>
                </el-form-item>
                <el-form-item label="所属租户" prop="tenant">
                    <!-- <el-input v-model="editForm.tenant"></el-input>-->
                    <el-select v-model="editForm.tenantId" placeholder="请选择租户"
                               style="width:100%">
                        <el-option v-for="item in selectTenantList"
                                   :key="item.i.id"
                                   :label="item.i.companyName"
                                   :value="item.i.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="客户状态">
                    <el-radio-group v-model="editForm.state" auto-complete="off">
                        <el-radio class="radio" :label="0">移入客户列表</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="客户姓名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="年龄">
                    <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
                </el-form-item>

                <el-form-item label="性别">
                    <el-radio-group v-model="addForm.gender">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="电话">
                    <el-input v-model="addForm.tel"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
                <el-form-item label="QQ">
                    <el-input v-model="addForm.qq"></el-input>
                </el-form-item>
                <el-form-item label="微信">
                    <el-input v-model="addForm.wechat"></el-input>
                </el-form-item>
                <el-form-item label="销售专员">
                    <el-input v-model="addForm.seller"></el-input>
                </el-form-item>
                <el-form-item label="职业">
                    <el-input v-model="addForm.job"></el-input>
                </el-form-item>
                <el-form-item label="收入水平">
                    <el-input v-model="addForm.salaryLevel"></el-input>
                </el-form-item>
                <el-form-item label="客户来源">
                    <el-input v-model="addForm.customerSource"></el-input>
                </el-form-item>
                <el-form-item label="录入人">
                    <el-input v-model="addForm.inputUser"></el-input>
                </el-form-item>
                <!-- <el-form-item label="录入时间">
                     <el-date-picker
                             type="date" placeholder="选择日期" v-model="addForm.inputTime"></el-date-picker>
                 </el-form-item>-->
                <el-form-item label="录入时间">
                    <el-date-picker type="datetime" placeholder="请选择时间"
                                    v-model="addForm.inputTime"
                                    format="yyyy-MM-dd HH:mm:ss"/>
                </el-form-item>
                <el-form-item label="租户" prop="tenant">
                    <!--v-model="addForm.parentId"对应:value="item.i.id"-->
                    <el-select v-model="addForm.tenantId" placeholder="请选择租户"
                               style="width:100%">
                        <el-option v-for="item in selectTenantList" :key="item.i.id" :label="item.i.companyName" :value="item.i.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="客户状态" >
                    <el-radio-group v-model="addForm.state" auto-complete="off">
                        <el-radio class="radio" :label="0">录入</el-radio>
                        <el-radio class="radio" :label="-1">放入资源池</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    /*git测试,增加此注释*/
    export default {
        data() {
            return {
                filters: {
                    name: ''
                },
                customers: [],
                total: 0,
                currentPage: 1,
                listLoading: false,//如果等于false 现在加载中效果
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,//修改加载中
                editFormRules: {
                    name: [
                        { required: true, message: '请输入名称', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: 0,
                    name: '',
                    age:'',
                    gender: 0,
                    tel:'',
                    email:'',
                    qq:'',
                    wechat:'',
                    seller:'',
                    job:'',
                    salaryLevel:'',
                    customerSource:'',
                    inputUser:'',
                    inputTime: '',
                    tenantId:'',
                    tenant:'',
                    state: 0
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,//新增加载
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                addForm: {
                    id: 0,
                    name: '',
                    age:'',
                    gender: 0,
                    tel:'',
                    email:'',
                    qq:'',
                    wechat:'',
                    seller:'',
                    job:'',
                    salaryLevel:'',
                    customerSource:'',
                    inputUser:'',
                    inputTime: '',
                    tenant: '',
                    tenantId:'',
                    state: 0
                }

            }
        },
        computed:{
            selectTenantList() {
                let obj = this.selectBox('/tenant/list');
                console.debug(obj)
                return obj;
            },
        },
        methods: {
            selectBox(getPath) {
                let obj = [];
                this.$http.patch(getPath).then(res => {
                    /*封装员工数据*/
                    res.data.forEach((item, index) => {
                        obj.push({
                            i: item
                        });
                    });
                });
                return obj;
            },
            //性别显示转换
            formatSex: function (row, column) {
                return row.gender == 1 ? '男' : row.gender == 0 ? '女' : '未知';
            },
            //状态显示转换
            formatState: function (row, column) {
                return row.state == 0 ? '录入' : row.state == -1 ? '放入资源池' : '未知';
            },
            handleCurrentChange(val){
                //当前页
                this.currentPage = val;
                this.getCustomers();
            },
            //获取用户列表
            getCustomers() {
                //发送axios请求到后端 查询部门
                // http://localhost:80/customer/list
                let param = {
                    name:this.filters.name,
                    currentPage: this.currentPage
                }
                this.$http.patch('/customer/queryPage1',param).then((res) => {
                    this.total = res.data.total;
                    this.customers = res.data.rows;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete('/customer/delete/'+row.id).then((res) => {
                        let {success,msg} = res.data;
                        this.listLoading = false;
                        if (success){
                            this.$message({
                                message: msg,
                                type: 'success'
                            });
                            this.getCustomers();
                        }else{
                            this.$message({
                                message: msg,
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {

                });
            },

            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                this.editForm.tenantId = row.tenant.companyName
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            this.$http.post('/customer/update',para).then((res) => {
                                let {success,msg} = res.data;
                                this.editLoading = false;
                                if (success){
                                    this.$message({
                                        message: msg,
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getCustomers();
                                }else{
                                    this.$message({
                                        message: msg,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            this.$http.put('/customer/save',para).then((res) => {
                                /*解构*/
                                let {success,msg} = res.data;
                                this.addLoading = false;
                                //判断是否成功
                                if (success){
                                    this.$message({
                                        message: msg,
                                        type: 'success'
                                    });
                                    /*清空表单*/
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                    /*刷新数据*/
                                    this.getCustomers();
                                }else {
                                    this.$message({
                                        message: msg,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            }
        },
        mounted() {
            this.getCustomers();
        }
    }

</script>

<style scoped>

</style>